package com.wk.code.wkserver.controller.app;


import com.wk.code.wkserver.controller.common.BaseController;
import com.wk.code.wkserver.request.app.WxLoginRequest;
import com.wk.code.wkserver.response.app.AppUserInfoResponse;
import com.wk.code.wkserver.response.result.R;
import com.wk.code.wkserver.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * User 控制层
 *
 * @Author wq
 * @Date 2021-03-16
 **/
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Api(tags = "app-前台用户登录管理")
public class LoginController extends BaseController {

    private final IUserService userService;


    /**
     * 微信授权登录
     *
     * @param request 请求参数
     * @return R
     */
    @ApiOperation(value = "微信登录")
    @PostMapping("/login/code")
    public R<String> loginCode(@RequestBody WxLoginRequest request) {
        return R.success(userService.loginCode(request));
    }

    /**
     * 微信手机号登录
     *
     * @param request 请求参数
     * @return R
     */
    @ApiOperation(value = "微信手机号一键登录")
    @PostMapping("/login/mobile")
    public R<AppUserInfoResponse> loginMobile(@RequestBody WxLoginRequest request) throws Exception {
        return R.success(userService.loginMobile(request));
    }

    /**
     * 获取用户信息
     *
     * @return AppUserInfoResponse
     */
    @ApiOperation(value = "登录成功获取用户信息")
    @GetMapping("/userInfo")
    public R<AppUserInfoResponse> userInfo() {
        return R.success(userService.userInfo(getUserId()));
    }

    /**
     * 退出登录
     *
     * @param request 请求参数
     * @return R
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public R<Void> logout(HttpServletRequest request) {
        userService.logout(request);
        return R.success();
    }

}